package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.defecttracking.entity.Role;
import com.woniu.defecttracking.entity.RoleRule;
import com.woniu.defecttracking.entity.Rule;
import com.woniu.defecttracking.exception.RoleException;
import com.woniu.defecttracking.exception.RuleException;
import com.woniu.defecttracking.mapper.RoleMapper;
import com.woniu.defecttracking.mapper.RoleRuleMapper;
import com.woniu.defecttracking.mapper.RuleMapper;
import com.woniu.defecttracking.param.AddRoleRuleParam;
import com.woniu.defecttracking.service.RoleRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class RoleRuleServiceImpl extends ServiceImpl<RoleRuleMapper, RoleRule> implements RoleRuleService {


    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RuleMapper ruleMapper;

    /**
    * @作者: 倪西
    * @描述：新增rule的逻辑判断
    * @时间：2022/1/6 14:59
    * @参数：[addRoleRuleParam]
    * @返回：java.lang.Boolean
    **/
    @Override
    public Boolean saveRoleRule(AddRoleRuleParam addRoleRuleParam) throws RoleException, RuleException {
        Role role = roleMapper.selectById(addRoleRuleParam.getRoleId());
        if (null==role){
            throw new RoleException("角色不存在",2055);
        }
        remove(new QueryWrapper<RoleRule>().eq("role_id",addRoleRuleParam.getRoleId()));
        if (null==addRoleRuleParam.getRuleIds()||addRoleRuleParam.getRuleIds().size()<=0){
            return true;
        }
        List<Rule> rules = ruleMapper.selectBatchIds(addRoleRuleParam.getRuleIds());
        if (rules.size()!=addRoleRuleParam.getRuleIds().size()){
            throw new RuleException("有rule不存在",2072);
        }
        List<Integer> ruleIds = addRoleRuleParam.getRuleIds();
        List<RoleRule> roleRules = new ArrayList<>();
        for (Integer id:ruleIds){
            RoleRule roleRule = new RoleRule();
            roleRule.setRoleId(addRoleRuleParam.getRoleId());
            roleRule.setRuleId(id);
            roleRules.add(roleRule);
        }
        return saveBatch(roleRules);
    }

    /**
    * @作者: 倪西
    * @描述：查询该角色对应的rule信息
    * @时间：2022/1/6 15:00
    * @参数：[id]
    * @返回：java.util.List<java.lang.Integer>
    **/
    @Override
    public List<Integer> findRuleIds(Integer id) throws RoleException {
        Role role = roleMapper.selectById(id);
        if (null==role){
            throw new RoleException("角色不存在",2055);
        }
        if (role.getRoleIsdel()==1){
            throw  new RoleException("角色已删除",2056);
        }
        List<RoleRule> roleRules = list(new QueryWrapper<RoleRule>().eq("role_id", id));
        List<Integer> ruleIds = roleRules.stream()
                .map(item -> item.getRuleId()).collect(Collectors.toList());
        return ruleIds;
    }
}
